package com.cwt_taxi_xa;

import android.location.Location;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.TextView;

import com.cwt_taxi_xa.constants.MyStatus;
import com.cwt_taxi_xa.util.DecimalUtil;
import com.cwt_taxi_xa.util.GPSUtil;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;

public class ShowGpsInfo extends CWTBaseActivity{

	private static final String TAG = "ShowGpsInfo";
	private TextView tv_status,tv_speed,tv_direction,tv_altitude,
			tv_lng,tv_lat,tv_time;
	private Handler handler;
	private Timer timer;
	private MyTask task;
	private SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.gpsinfo);

		setupViews();
		createHandler();
		initData();
	}

	private void createHandler() {
		// TODO Auto-generated method stub
		handler = new Handler(){
			@Override
			public void handleMessage(Message msg) {
				// TODO Auto-generated method stub
				super.handleMessage(msg);
				if(msg.what == 1001){
					showGpsData();
				}
			}
		};
	}

	protected void showGpsData() {
		// TODO Auto-generated method stub
		if(MyStatus.gps){
			tv_status.setText("已定位");
			try {
				Location locationInfo = GPSUtil.getInstance().locationInfo;
				if(locationInfo != null){
					long time = locationInfo.getTime();
					double lng = locationInfo.getLongitude();
					double lat = locationInfo.getLatitude();
					double altitude = locationInfo.getAltitude();
					double speed = (double)locationInfo.getSpeed() * 36f;
					float direction = locationInfo.getBearing();

					String gpsTime = df.format(new Date(time));
					tv_time.setText(gpsTime);
					tv_altitude.setText(""+DecimalUtil.priceFormat(altitude)+" 米");
					tv_direction.setText(""+DecimalUtil.priceFormat(direction));
					tv_lat.setText(""+DecimalUtil.gpsFormat(lat));
					tv_lng.setText(""+DecimalUtil.gpsFormat(lng));
					tv_speed.setText(""+DecimalUtil.priceFormat(speed)+" KM/h");
				}
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}else{
			tv_status.setText("未定位");
			tv_altitude.setText("--");
			tv_direction.setText("--");
			tv_lat.setText("--");
			tv_lng.setText("--");
			tv_speed.setText("--");
			tv_time.setText("--");
		}
	}

	private void setupViews() {
		// TODO Auto-generated method stub
		tv_status = (TextView)findViewById(R.id.tv_status);
		tv_speed = (TextView)findViewById(R.id.tv_speed);
		tv_direction = (TextView)findViewById(R.id.tv_direction);
		tv_altitude = (TextView)findViewById(R.id.tv_altitude);
		tv_lng = (TextView)findViewById(R.id.tv_lng);
		tv_lat = (TextView)findViewById(R.id.tv_lat);
		tv_time = (TextView)findViewById(R.id.tv_time);
	}

	private class MyTask extends TimerTask{

		@Override
		public void run() {
			// TODO Auto-generated method stub
			handler.obtainMessage(1001).sendToTarget();
		}
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
	}

	public void initData(){
		startTimer();
	}

	private void startTimer() {
		// TODO Auto-generated method stub
		stopTimer();
		timer = new Timer();
		task = new MyTask();
		timer.schedule(task, 500, 3000);
	}

	private void stopTimer() {
		// TODO Auto-generated method stub
		if(task != null){
			task.cancel();
			task = null;
		}
		if(timer != null){
			timer.cancel();
			timer = null;
		}
	}

	@Override
	public void setStatus(int index, boolean status) {
		// TODO Auto-generated method stub

	}

	@Override
	public void sendMsg(int type, Object obj) {
		// TODO Auto-generated method stub

	}
}
